package com.app.controller;

import com.app.common.Result;
import com.app.mapper.MatterMapper;
import com.app.model.*;
import com.app.service.*;
import org.junit.After;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;

//督书记、书记督
@Controller
@RestController
@RequestMapping("/api/supervision")
public class SupervisionController {
    @Autowired
    private SupervisionService supervisionService;
    @Autowired
    private ReportService reportService;
    @Autowired
    private MatterService matterService;
    @Autowired
    private ProgressService progressService;
    @Autowired
    private SearchService searchService;

    //督书记
    @GetMapping("/superintendentSecretary")
    public Result<Object> superintendentSecretary(Integer id, String startTime,Integer type) {
        return Result.success(supervisionService.superintendentSecretary(id, startTime,type));
    }

    //督书记按pid查书记
    @GetMapping("/getByPid")
    public Result<Object> getByPid(Integer id) {
        return Result.success(supervisionService.getByPid(id));
    }

    //小程序
    //我的动态 /api/supervision/getAll
    @GetMapping("/getAll")
    public Result<Object> getAll(Integer id, String name, String startTime,Integer type) {
        return Result.success(supervisionService.getAll(id, name, startTime,type));
    }

    @GetMapping("/selectDutiesByName")
    public Result<Object> selectDutiesByName(String name) {
        return Result.success(supervisionService.selectDutiesByName(name));
    }

    //后台管理
    //查询党组织分类
    @GetMapping("/selectType")
    public Result<Object> selectType() {
        return Result.success(supervisionService.selectType());
    }

    //添加和编辑report
    @PostMapping("/insertAndUpdateReport")
    public Result<Object> insertAndUpdateReport(@RequestBody ReportModel reportModel) {
        if (reportModel.getId() == 0) {
            reportModel.setCreateTime(LocalDateTime.now());
            reportService.insert(reportModel);
        } else {
            reportModel.setUpdateTime(LocalDateTime.now());
            reportService.update(reportModel);
        }
        return Result.success("操作成功");
    }

    //删除
    @GetMapping("/deleteReport")
    public Result<Object> deleteReportById(int id) {
        reportService.deleteReportById(id);
        return Result.success("操作成功！");
    }

    //查询
    @GetMapping("/getList")
    public Result<Object> getList(Integer page, Integer limit, Integer pid, String report) {
        return Result.success(supervisionService.getList(page, limit, pid, report));
    }

    //书记督  /api/supervision/selectAll
    //小程序
    @GetMapping("/selectAll")
    public Result<Object> selectAll(String name, Integer type, String startTime, String endTime,Integer dutiesId) {
        return Result.success(supervisionService.selectAll(name, type, startTime, endTime,dutiesId));
    }

    //后台管理
    //添加和编辑督查事项
    @PostMapping("/insertAndUpdateMatter")
    public Result<Object> insertAndUpdateMatter(@RequestBody MatterModel matterModel) {
        if (matterModel.getId() == 0) {
            matterModel.setCreateTime(LocalDateTime.now());
            matterModel.setProgressTime(String.format(LocalDateTime.now().toString(),"%tF"));
            matterService.insert(matterModel);
        }
        count();
        return Result.success("操作成功");
    }

    //删除督查事项
    @GetMapping("/deleteMatter")
    public Result<Object> deleteMatterById(int id) {
        matterService.deleteMatterById(id);
        return Result.success("操作成功！");
    }


    //添加和编辑进展情况
    @PostMapping("/insertAndUpdateProgress")
    public Result<Object> insertAndUpdateProgress(@RequestBody ProgressModel progressModel) {
        MatterModel matterModel = new MatterModel();
        progressModel.setCreateTime(LocalDateTime.now());
        progressService.insert(progressModel);
        matterModel.setId(progressModel.getMatterId());
        matterModel.setProgressTime(progressModel.getTime());
        matterModel.setUpdateTime(progressModel.getTime());
        matterService.updateTime(matterModel);
        return Result.success("操作成功");
    }

    //删除进展情况
    @GetMapping("/deleteProgress")
    public Result<Object> deleteProgressById(int id) {
        progressService.deleteProgressById(id);
        return Result.success("操作成功！");
    }

    //查询进展情况
    @GetMapping("/selectProgress")
    public Result<Object> selectProgress(Integer matterId) {
        return Result.success(supervisionService.selectProgress(matterId));
    }

    //查询
    @GetMapping("/selectList")
    public Result<Object> selectList(Integer page, Integer limit, Integer pid, String matterName) {
        return Result.success(supervisionService.selectList(page, limit, pid, matterName));
    }

    //红点统计
    private void count() {
        supervisionService.redCount();
    }

    private Integer getCount() {
        return supervisionService.getCount();
    }

    @GetMapping("/redCount")
    public Result<Object> redCount() {
        return Result.success(getCount());
    }


    //根据时间筛选
    @GetMapping("/selectByTime")
    public Result<Object> selectByTime(String startTime, String endTime) {
        supervisionService.selectByTime(startTime, endTime);
        return Result.success(1);
    }
}
